package cn.tedu.com.config;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.net.UnknownHostException;

@Configuration
public class RedisConfig {

    @ConfigurationProperties(value = "spring.rdb2.redis")
    @Bean
    public RedisProperties rpOne(){
        return new RedisProperties();
    }
    @ConfigurationProperties(value = "spring.redis")
    @Bean
    public RedisProperties rpZero(){
        return new RedisProperties();
    }

    @Bean
    public RedisConnectionFactory rcfZero(RedisProperties rpZero){
        RedisStandaloneConfiguration redisStandaloneConfiguration=new RedisStandaloneConfiguration();
        // 为集群配置使用
//        RedisClusterConfiguration redisClusterConfiguration=new RedisClusterConfiguration();
        //哨兵模式
//        RedisSentinelConfiguration redisSentinelConfiguration=new RedisSentinelConfiguration();
        redisStandaloneConfiguration.setHostName(rpZero.getHost());
        redisStandaloneConfiguration.setPassword(rpZero.getPassword());
        redisStandaloneConfiguration.setPort(rpZero.getPort());
        redisStandaloneConfiguration.setDatabase(rpZero.getDatabase());
        GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
        LettucePoolingClientConfiguration lettucePoolingClientConfiguration= LettucePoolingClientConfiguration.builder()
                .poolConfig(poolConfig)
                .build();
        LettuceConnectionFactory lettuceConnectionFactory=new LettuceConnectionFactory(redisStandaloneConfiguration,lettucePoolingClientConfiguration);

        return lettuceConnectionFactory;
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory rcfZero)
            throws UnknownHostException {
        StringRedisSerializer srs=new StringRedisSerializer();
        Jackson2JsonRedisSerializer<Object> jjrs = new Jackson2JsonRedisSerializer<>(Object.class);
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(rcfZero);
        template.setKeySerializer(srs);
        template.setValueSerializer(jjrs);
        template.setHashKeySerializer(srs);
        template.setHashValueSerializer(jjrs);
        return template;
    }

    @Bean
    public StringRedisTemplate srtZero(RedisConnectionFactory rcfZero)
            throws UnknownHostException {
        StringRedisSerializer srs=new StringRedisSerializer();
        StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
        stringRedisTemplate.setConnectionFactory(rcfZero);
        stringRedisTemplate.setKeySerializer(srs);
        stringRedisTemplate.setValueSerializer(srs);
        return stringRedisTemplate;
    }
    // redis one 库
    @Bean
    public RedisConnectionFactory rcfOne(RedisProperties rpOne){
        RedisStandaloneConfiguration redisStandaloneConfiguration=new RedisStandaloneConfiguration();
        // 为集群配置使用
//        RedisClusterConfiguration redisClusterConfiguration=new RedisClusterConfiguration();
        //哨兵模式
//        RedisSentinelConfiguration redisSentinelConfiguration=new RedisSentinelConfiguration();
        redisStandaloneConfiguration.setHostName(rpOne.getHost());
        redisStandaloneConfiguration.setPassword(rpOne.getPassword());
        redisStandaloneConfiguration.setPort(rpOne.getPort());
        redisStandaloneConfiguration.setDatabase(rpOne.getDatabase());
        GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
        LettucePoolingClientConfiguration lettucePoolingClientConfiguration= LettucePoolingClientConfiguration.builder()
                .poolConfig(poolConfig)
                .build();
        LettuceConnectionFactory lettuceConnectionFactory=new LettuceConnectionFactory(redisStandaloneConfiguration,lettucePoolingClientConfiguration);

        return lettuceConnectionFactory;
    }

    @Bean
    public RedisTemplate<String, Object> rtOne(RedisConnectionFactory rcfOne)
            throws UnknownHostException {
        StringRedisSerializer srs=new StringRedisSerializer();
        Jackson2JsonRedisSerializer<Object> jjrs = new Jackson2JsonRedisSerializer<>(Object.class);
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(rcfOne);
        template.setKeySerializer(srs);
        template.setValueSerializer(jjrs);
        template.setHashKeySerializer(srs);
        template.setHashValueSerializer(jjrs);
        return template;
    }

    @Bean
    public StringRedisTemplate srtOne(RedisConnectionFactory rcfOne)
            throws UnknownHostException {
        StringRedisSerializer srs=new StringRedisSerializer();
        StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
        stringRedisTemplate.setConnectionFactory(rcfOne);
        stringRedisTemplate.setKeySerializer(srs);
        stringRedisTemplate.setValueSerializer(srs);
        return stringRedisTemplate;
    }
}
